package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopGoods;
import cn.regexp.domain.ShopIngredient;
import cn.regexp.query.IngredientQuery;
import cn.regexp.service.IIngredientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

/**
 * 食材控制器
 */
@Slf4j
@Controller
@RequestMapping("business/ingredients")
public class IngredientController {
    //模板前缀
    private static final String prefix = "business/ingredients/";

    @Autowired
    private IIngredientService ingredientsService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:ingredients:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:ingredients:add")
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        return prefix + "add";
    }

    @RequiresPermissions("business:ingredients:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", ShopGoods.DELETE_NO);
        model.addAttribute("ingredient", ingredientsService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:ingredients:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopIngredient> query(IngredientQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopIngredient> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(qo.getState()), "state", qo.getState())
                .eq(ObjectUtil.isNotEmpty(qo.getType()), "type", qo.getType())
                .like(ObjectUtil.isNotEmpty(qo.getKeyword()), "ingredient_name", qo.getKeyword());
        return new TablePageInfo<>(ingredientsService.list(wrapper));
    }

    //新增
    @RequiresPermissions("business:ingredients:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopIngredient ingredient) {
        ingredientsService.save(ingredient);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:ingredients:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopIngredient ingredient) {
        ingredientsService.updateById(ingredient);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:ingredients:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        ingredientsService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }

    // 上架
    @RequiresPermissions("business:ingredients:saleOn")
    @GetMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id) {
        UpdateWrapper<ShopIngredient> wrapper = new UpdateWrapper<>();
        wrapper.set("state", ShopIngredient.Ingredient_UP)
                .eq("id", id);
        ingredientsService.update(wrapper);
        return AjaxResult.success();
    }

    // 下架
    @RequiresPermissions("business:ingredients:saleOff")
    @GetMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id) {
        UpdateWrapper<ShopIngredient> wrapper = new UpdateWrapper<>();
        wrapper.set("state", ShopIngredient.Ingredient_DOWN)
                .eq("id", id);
        ingredientsService.update(wrapper);
        return AjaxResult.success();
    }
}
